package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.CustomerTransfer;
import cn.wolfcode.rbac.mapper.CustomerMapper;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ICustomerService;
import cn.wolfcode.rbac.service.ICustomerTransferService;
import cn.wolfcode.rbac.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customerTransfer")
public class CustomerTransferController {

    @Autowired
    private ICustomerTransferService customerTransferService;

    @Autowired
    private CustomerMapper customerMapper;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        model.addAttribute("result", this.customerTransferService.query(qo));
        return "customerTransfer/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CustomerTransfer entity) {
        try {
            this.customerTransferService.saveOrUpdate(entity);
            customerMapper.updateSeller(entity.getCustomerId(), entity.getNewSellerId());
            return new JsonResult("操作成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("操作失败", false);
        }
    }

    @RequestMapping("/delete")
    public String delete(Long id, Model model) {
        this.customerTransferService.delete(id);
        return "redirect:/customerTransfer/list.do";
    }

}
